//
//  JZHJobComViewController.m
//  极致荟
//
//  Created by hetao on 14-8-21.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHJobComViewController.h"

@interface JZHJobComViewController ()
{
    NSDictionary *datadic;
}
@end

@implementation JZHJobComViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    datadic=[[NSDictionary alloc] init];
    self.comname.text=[User defuceUser].company;
    [self.comname becomeFirstResponder];
    [self.jobname setInputView:self.key];
    self.jobname.text=[[User defuceUser].jobdic objectForKey:@"name"];
    [self httpdata];
    self.navigationItem.title=@"公司信息";
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    // Do any additional setup after loading the view from its nib.
    UIButton *rightButton=[[UIButton alloc]init];
     [rightButton setFrame:CGRectMake(0, 0, 50, 10)];
    [rightButton addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [rightButton setTitleColor:TEXTBULE forState:UIControlStateNormal];
    [rightButton setFont:[UIFont systemFontOfSize:18]];
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)back:(id)sender

{

    [self.navigationController popViewControllerAnimated:YES];
}


- (void)httpdata
{
    [HttpRequest httpRequestPost:JZHHTTPURL(@"myInformation/jobList") parameters:nil block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            datadic=[data objectForKey:@"data"];
            [self.pickea reloadAllComponents];
        }
    }];
}
- (void)save:(id)sender
{
    [self upUserData:[datadic allKeysForObject:self.jobname.text][0] key:@"Job"];
    [self upUserData:self.comname.text key:@"Company"];
   [self.navigationController popViewControllerAnimated:YES];
    
}

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return datadic.count;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *lab;
    if(!view)
    {
        lab=[[UILabel alloc] init];
    }else
    {
        lab=(UILabel *)view;
    }
    
    lab.textAlignment=NSTextAlignmentCenter;
    lab.text=datadic.allValues[row];
    return lab;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.jobname.text=datadic.allValues[row];
}

- (void)upUserData:(NSString *)value key:(NSString *)key
{
    NSString *comstr=[self.comname.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if(comstr.length==0 || self.jobname.text.length==0)
    {
        return;
    }
   
    [User defuceUser].company=self.comname.text;
    [User defuceUser].jobdic=[[NSDictionary alloc] initWithObjectsAndKeys:self.jobname.text,@"name",[datadic allKeysForObject:self.jobname.text][0],@"id", nil];
    NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId",key,@"infoType",value,@"editInfo",nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"myInformation/editInfo") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        NSLog(@"%@",data);
    }];
    
}
@end
